<?php
    $porto_helper = $this->helper('Smartwave\Porto\Helper\Data');
    $product_view = $porto_helper->getConfig('porto_settings/product');
    $_helper = $this->helper('Magento\Catalog\Helper\Output');
    $_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
    $sticky_header = "no-sticky-header";
    if($porto_helper->getConfig('porto_settings/header/sticky_header')) {
        $sticky_header = "";
    }
    $_product = $block->getProduct();
    if(isset($product_view['enable_addtocart_sticky']) && $product_view['enable_addtocart_sticky']) {
        $productImage = $_imagehelper->init($_product, 'category_page_grid')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(50);
        $productImageUrl = $productImage->getUrl();
?>
<div class="sticky-product hide <?php echo $sticky_header; ?>">
    <div class="container">
        <div class="sticky-image">
           <img class="product-image-photo default_image" src="<?php echo $productImageUrl; ?>" alt="<?php echo $productImage->getLabel(); ?>"/>
        </div>
        <div class="sticky-detail">
            <div class="product-name-area">
                <h2 class="product-name"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name'); ?></h2>
                <div class="product-info-price"></div>
            </div>
        </div>
        <?php $buttonTitle = __('Add to Cart'); ?>
        <?php if ($_product->isSaleable()): ?>
        <div class="actions">
            <button type="button"
                    title="<?php /* @escapeNotVerified */ echo $buttonTitle ?>"
                    class="action primary tocart" id="product-addtocart-button-clone">
                <span><?php /* @escapeNotVerified */ echo $buttonTitle ?></span>
            </button>
            <?php echo $block->getChildHtml('', true) ?>
        </div>
        <?php endif; ?>
    </div>
</div>
<script type="text/javascript">
require([
    'jquery'
], function ($) {
    var p_scrolled = false;
    $(window).scroll(function(){
        if(400<$(window).scrollTop() && !p_scrolled){
            p_scrolled = true;
            $('.product-info-main .product-info-price > *').each(function(){
                $(this).parent().append($(this).clone());
                var tmp = $(this).detach();
                $('.sticky-product .product-info-price').append(tmp);
            });
            $(".sticky-product").removeClass("hide");
            $("#product-addtocart-button").off("DOMSubtreeModified").on("DOMSubtreeModified",function(){
                $("#product-addtocart-button-clone").html($(this).html());
                $("#product-addtocart-button-clone").attr("class",$(this).attr("class"));
            });
        }
        if(400>=$(window).scrollTop() && p_scrolled){
            p_scrolled = false;
            $('.product-info-main .product-info-price > *').remove();
            $('.sticky-product .product-info-price > *').each(function(){
                var tmp = $(this).detach();
                $('.product-info-main .product-info-price').append(tmp);
            });
            $(".sticky-product").addClass("hide");
        }
    });
    $("#product-addtocart-button-clone").click(function(){
        $("#product-addtocart-button").trigger("click");
    });
});
</script>
<?php
    }
?>
<?php if(isset($product_view['move_tab']) && $product_view['move_tab']): ?>
<script type="text/javascript">
require([
    'jquery'
], function ($) {
    $(document).ready(function(){
        $(".product.info.detailed").detach().appendTo($(".product-info-main"));
    });
});
</script>
<?php endif; ?>